let requestUrl;

function addHandler() {
	requestUrl = "/chongwu_waiguan_category/insert";
	
	$("#waiguan_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#waiguan_list").datagrid("getSelected");
	
	if (rowData) {
		requestUrl = "/chongwu_waiguan_category/updateById";
		
		$("#id").val(rowData.id);
		$("#name").textbox("setValue", rowData.name);
		$("#note").textbox("setValue", rowData.note);
		$("#propertyId").combobox("setValue", rowData.propertyId);
		$("#propertyValue").numberspinner("setValue", rowData.propertyValue);

		$("#waiguan_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

$(document).ready(function() {

	$("#property_id").combobox({
		url: "/property/selectByType?type=2",
		valueField: "id",
		textField: "name",
		width: 150,
		prompt: "-选择属性-",
		panelHeight: "auto"
	});

	$("#search").linkbutton({
		iconCls: "icon-search"
	}).click(function() {
		let propertyId = $("#property_id").combobox("getValue");

		$("#waiguan_list").datagrid("reload", {
			propertyId: propertyId
		});
	});

	$("#clear").linkbutton({
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	/******************************************************************************************************/

	$("#name").textbox({
		width: 120,
		required: true
	});

	$("#propertyId").combobox({
		url: "/property/selectByType?type=2",
		valueField: "id",
		textField: "name",
		width: 120,
		required: true,
		panelHeight: "auto"
	});

	$("#propertyValue").numberspinner({
		min: 3,
		max: 5,
		width: 120,
		required: true
	});

	$("#note").textbox({
		width: 320,
		height: 80,
		required: true,
		multiline: true
	});

	$("#waiguan_dialog").dialog({
		title: "宠物外观信息",
		width: 500,
		height: 300,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#waiguan_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#waiguan_dialog").dialog("close");
						$("#waiguan_list").datagrid("reload");
					}, error);
				});
			}
		},{
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#waiguan_dialog").dialog("close");
				$("#waiguan_form").form("clear");
			}
		}]
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr * 15;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;
	/**
	 * 可选页码
	 * @type {number[]}
	 */
	let pageList = [15, 30, 100, 200, 500]

	$("#waiguan_list").datagrid({
		url: "/chongwu_waiguan_category/selectByPage",
		method: "get",
		height: height_table,
		rownumbers: true,
		fitColumns: true,
		pagination: true,
		pageList: pageList,
		pageSize: pageList[0],
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}],
		columns: [[
			{field: "id", hidden: true},
			{field: "name", title: "外观名称", align:"center", width: 100},
			{field: "note", title: "外观说明", align:"center", width: 200},
			{field: "propertyId", title: "外观属性", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/property/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#property_" + rowIndex).html(data.name);
					}, error);

					return "<div id='property_" + rowIndex + "'></div>";
				}
			},
			{field: "propertyValue", title: "属性值", align: "center", width: 100}
		]]
	});

});